package xin.xihc.demo.dao;

import org.springframework.stereotype.Repository;
import xin.xihc.demo.model.UserTest;
import xin.xihc.jba.core.PageInfo;
import xin.xihc.jba.sql.Select;
import xin.xihc.jba.sql.Update;
import xin.xihc.jba.sql.Where;
import xin.xihc.jba.sql.clause.Eq;
import xin.xihc.jba.sql.clause.EqSelf;
import xin.xihc.jba.sql.clause.In;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * UserTestDao
 *
 * @author Leo.Xi
 * @version 1.0
 * @date 2019/2/11 20:19
 * @since 1.0
 */
@Repository
public class UserTestDao extends BaseDao<UserTest> {

    public void updateTest() {
        Update update = Update.from(UserTest.class).set(new EqSelf("age", 1));
        jbaTemplate.update(update);
    }

    /**
     * 字符串拼接sql、不能防止SQL注入
     *
     * @return
     */
    public UserTest findByNameConcat(String name) {
        UserTest userTest = jbaTemplate.queryOne("select * from T_USER where name = '" + name + "'", null, UserTest.class);
        return userTest;
    }

    /**
     * JDBCTemplate自动操作
     *
     * @return
     */
    public UserTest findByName(String name) {
        Select select = Select.from(UserTest.class).select(UserTest.class).where(new Where().and(new Eq("name", name)));
        UserTest userTest = jbaTemplate.queryOne(select, UserTest.class);
        return userTest;
    }

    public List<UserTest> testIn(String... ids) {
        Select select = Select.from(UserTest.class).select(UserTest.class).where(new Where().and(new In("id",
                Arrays.asList(ids)))).orderBy("cid desc");
        return jbaTemplate.queryList(select, UserTest.class, new PageInfo());
    }

    public List<UserTest> page(List<String> ids, PageInfo pageInfo) {
        String sql = "SELECT * FROM T_USER WHERE id IN (:ids)";
        HashMap<String, Object> params= new HashMap<>(1);
        params.put("ids", ids);
        return jbaTemplate.queryList(sql, params, UserTest.class, pageInfo);
    }
}
